import {deepClone} from "../utils/func";
import {ADD_LIST, CHANGE_VALUE,INIT_LIST} from "../constants/action-type";

const defaultState = {
    value: "代办",
    list: [
        "做运动",
        "写作业"
    ]
};
/**
 *
 * @param state 就是我们现在的state值
 * @param action 发送的action
 * @returns {*} 返回一个新的state
 */
function reducer(state = defaultState, action) {
    // 使用工具类 进行深拷贝数据
    const newState = deepClone(state);
    // 判断 action发起的行为的类型
    switch (action.type) {
        case CHANGE_VALUE:
            newState.value = action.value;
            return newState;
        case ADD_LIST:
            newState.list.push(action.value);
            newState.value = '';
            return newState;
        case INIT_LIST:
            newState.list = action.list
            return newState;
        default:
            return state
    }
}

export default reducer;